﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;
using System.Drawing;
using System.Xml.XPath;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using efuture.xml;


namespace efuture.vdrp
{
    public class GridRender
    {

        private static GridColumn GetNewColumn(GridView gridView1, XPathNavigator node)
        {
            Type t = null;

            GridColumn gc = gridView1.Columns.Add();
            gc.Tag = node.SelectSingleNode("Aggregation") == null ? "None" : node.SelectSingleNode("Aggregation").Value;
            gc.Name = node.SelectSingleNode("Name").Value;
            gc.FieldName = node.SelectSingleNode("FieldName").Value;
            gc.Caption = node.SelectSingleNode("Caption").Value;
            gc.Visible = true;
            gc.VisibleIndex = node.SelectSingleNode("VisibleIndex").ValueAsInt;
            gc.Width = node.SelectSingleNode("Width").ValueAsInt;
            gc.MinWidth = gc.Width;
            gc.OptionsColumn.AllowEdit = false;
            //ColumnMap.Add(gridColumn.FieldName, gridColumn);

            // DisplayFormat
            String FormatString = node.SelectSingleNode("FormatString").Value;
            if (!String.IsNullOrEmpty(FormatString))
            {
                gc.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                gc.DisplayFormat.FormatString = FormatString;
            }

            // SummaryType
            String v = node.SelectSingleNode("SummaryType").Value;
            if (!String.IsNullOrEmpty(v))
            {
                t = typeof(DevExpress.Data.SummaryItemType);
                gc.SummaryItem.SummaryType = (DevExpress.Data.SummaryItemType)Enum.Parse(t, v);
                gc.SummaryItem.DisplayFormat = node.SelectSingleNode("SummaryDisplayFormat").Value;
                gc.SummaryItem.FieldName = node.SelectSingleNode("SummaryFieldName").Value;

                if (gc.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None)
                {
                    gridView1.OptionsView.ShowFooter = true;
                    gridView1.GroupSummary.Add(
                        gc.SummaryItem.SummaryType,
                        gc.FieldName,
                        gc,
                        gc.SummaryItem.DisplayFormat
                    );
                }
            }

            // SummaryItem
            String SummaryType = node.SelectSingleNode("SummaryType").Value;
            String SummaryDisplayFormat = node.SelectSingleNode("SummaryDisplayFormat").Value;
            t = typeof(DevExpress.Data.SummaryItemType);
            gc.SummaryItem.SummaryType = (DevExpress.Data.SummaryItemType)Enum.Parse(t, SummaryType);
            gc.SummaryItem.DisplayFormat = SummaryDisplayFormat;

            return gc;
        }

        public static void Render(GridView gridView1, String GridConfig)
        {
            if (String.IsNullOrEmpty(GridConfig))
            {
                return;
            }

            XPathNodeIterator nodes = null;
            Dictionary<String, GridColumn> ColumnMap = new Dictionary<string, GridColumn>();
            try
            {
                gridView1.BeginUpdate();
                XPathNavigator xNav = XMLHelper.CreateNavigator(GridConfig);
                nodes = xNav.Select("/Root/Columns/Column");
                if (nodes.Count > 0)
                {
                    foreach (XPathNavigator node in nodes)
                    {
                        if ("1".Equals(node.SelectSingleNode("Visible").Value))
                        {
                            GetNewColumn(gridView1, node);
                        }
                    }
                }
                gridView1.EndUpdate();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public static void Apply(GridView gridView1, String GridConfig)
        {
            if (String.IsNullOrEmpty(GridConfig))
            {
                return;
            }

            String v = "";
            Type t = null;
            XPathNodeIterator nodes = null;
            Dictionary<String, GridColumn> ColumnMap = new Dictionary<string, GridColumn>();
            foreach (GridColumn gc in gridView1.Columns)
            {
                ColumnMap.Add(gc.FieldName, gc);
            }

            try
            {
                XPathNavigator xNav = XMLHelper.CreateNavigator(GridConfig);

                gridView1.BeginUpdate();
                nodes = xNav.Select("/Root/Columns/Column");
                if (nodes.Count > 0)
                {
                    foreach (XPathNavigator node in nodes)
                    {
                        String FieldName = node.SelectSingleNode("FieldName").Value;

                        if (ColumnMap.ContainsKey(FieldName))
                        {
                            // UnboundType
                            v = node.SelectSingleNode("UnboundType").Value;
                            if (!String.IsNullOrEmpty(v))
                            {
                                t = typeof(DevExpress.Data.UnboundColumnType);
                                ColumnMap[FieldName].UnboundType = (DevExpress.Data.UnboundColumnType)Enum.Parse(t, v);
                            }

                            // UnboundExpression
                            v = node.SelectSingleNode("UnboundExpression").Value;
                            if (!String.IsNullOrEmpty(v))
                            {
                                ColumnMap[FieldName].UnboundExpression = v;
                                ColumnMap[FieldName].ShowUnboundExpressionMenu = true;
                            }

                            // VisibleIndex
                            ColumnMap[FieldName].VisibleIndex = node.SelectSingleNode("VisibleIndex").ValueAsInt;
                        }
                    }
                }

                // GroupedColumns
                nodes = xNav.Select("/Root/GroupedColumns/GroupedColumn");
                if (nodes.Count > 0)
                {
                    gridView1.OptionsView.ShowGroupPanel = true;
                    foreach (XPathNavigator node in nodes)
                    {
                        String FieldName = node.SelectSingleNode("FieldName").Value;
                        if (ColumnMap.ContainsKey(FieldName))
                        {
                            ColumnMap[FieldName].Group();
                        }
                    }
                }
                else
                {
                    gridView1.IndicatorWidth = 45;
                    gridView1.CustomDrawRowIndicator += GridViewCustomDrawRowIndicator;
                }

                // FormatConditions
                nodes = xNav.Select("/Root/FormatConditions/FormatCondition");
                if (nodes.Count > 0)
                {
                    FontConverter fc = new FontConverter();
                    foreach (XPathNavigator node in nodes)
                    {
                        String FieldName = node.SelectSingleNode("FieldName").Value;

                        if (ColumnMap.ContainsKey(FieldName))
                        {
                            StyleFormatCondition sfc = new StyleFormatCondition();
                            gridView1.FormatConditions.Add(sfc);

                            sfc.ApplyToRow = Boolean.Parse(node.SelectSingleNode("ApplyToRow").Value);
                            sfc.Column = ColumnMap[FieldName];
                            t = typeof(FormatConditionEnum);
                            sfc.Condition = (FormatConditionEnum)Enum.Parse(t, node.SelectSingleNode("Condition").Value);
                            String Value1 = node.SelectSingleNode("Value1").Value;
                            String TypeOfValue1 = node.SelectSingleNode("TypeOfValue1").Value;
                            String Value2 = node.SelectSingleNode("Value2").Value;
                            String TypeOfValue2 = node.SelectSingleNode("TypeOfValue2").Value;
                            if (!string.IsNullOrEmpty(Value1))
                            {
                                switch (TypeOfValue1)
                                {
                                    case "System.String":
                                        sfc.Value1 = Value1;
                                        break;
                                    case "System.Decimal":
                                        sfc.Value1 = Convert.ToDecimal(Value1);
                                        break;
                                    case "System.DateTime":
                                        sfc.Value1 = Convert.ToDateTime(Value1);
                                        break;
                                    default:
                                        break;
                                }
                            }
                            if (!string.IsNullOrEmpty(Value2))
                            {
                                switch (TypeOfValue2)
                                {
                                    case "System.String":
                                        sfc.Value2 = Value2;
                                        break;
                                    case "System.Decimal":
                                        sfc.Value2 = Convert.ToDecimal(Value2);
                                        break;
                                    case "System.DateTime":
                                        sfc.Value2 = Convert.ToDateTime(Value2);
                                        break;
                                    default:
                                        break;
                                }
                            }
                            sfc.Appearance.BackColor = ColorTranslator.FromHtml(node.SelectSingleNode("BackColor").Value);
                            sfc.Appearance.BackColor2 = ColorTranslator.FromHtml(node.SelectSingleNode("BackColor2").Value);
                            sfc.Appearance.Font = (Font)fc.ConvertFromString(node.SelectSingleNode("Font").Value);
                        }
                    }
                }

                // OptionsView
                nodes = xNav.Select("/Root/OptionsViews/OptionsView");
                if (nodes.Count > 0)
                {
                    foreach (XPathNavigator node in nodes)
                    {
                        String option = node.SelectSingleNode("Name").Value;
                        bool optionSet = "1".Equals(node.SelectSingleNode("Value").Value);

                        DevExpress.Utils.SetOptions.SetOptionValueByString(option, gridView1.OptionsView, optionSet);
                    }
                }

                gridView1.EndUpdate();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        private static void GridViewCustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (1 + e.RowHandle).ToString();
                e.Info.ImageIndex = -1;
            }
        }

    }

    public class PivotGridRender
    {

        private static PivotGridField GetNewField(PivotGridControl pivotGrid, XPathNavigator node)
        {
            Type t = null;

            PivotGridField field = pivotGrid.Fields.Add();
            field.Caption = node.SelectSingleNode("Caption").Value;
            field.FieldName = node.SelectSingleNode("FieldName").Value;
            field.Visible = "1".Equals(node.SelectSingleNode("Visible").Value);
            field.Tag = node.SelectSingleNode("Aggregation") == null ? "None" : node.SelectSingleNode("Aggregation").Value;

            t = typeof(PivotArea);
            field.Area = (PivotArea)Enum.Parse(t, node.SelectSingleNode("Area").Value);
            field.AreaIndex = node.SelectSingleNode("AreaIndex").ValueAsInt;
            field.Width = node.SelectSingleNode("Width").ValueAsInt;

            t = typeof(DevExpress.Data.UnboundColumnType);
            field.UnboundType = (DevExpress.Data.UnboundColumnType)Enum.Parse(t, node.SelectSingleNode("UnboundType").Value);
            field.UnboundExpression = node.SelectSingleNode("UnboundExpression").Value;
            field.Options.ShowUnboundExpressionMenu = !String.IsNullOrEmpty(field.UnboundExpression);

            t = typeof(DevExpress.Data.PivotGrid.PivotSummaryType);
            field.SummaryType = (DevExpress.Data.PivotGrid.PivotSummaryType)Enum.Parse(t, node.SelectSingleNode("SummaryType").Value);

            t = typeof(PivotTotalsVisibility);
            field.TotalsVisibility = (PivotTotalsVisibility)Enum.Parse(t, node.SelectSingleNode("TotalsVisibility").Value);

            field.CellFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            field.CellFormat.FormatString = node.SelectSingleNode("FormatString").Value;

            field.Options.AllowEdit = false;

            return field;
        }

        public static void Render(PivotGridControl pivotGrid, String GridConfig)
        {
            if (String.IsNullOrEmpty(GridConfig))
            {
                return;
            }

            XPathNodeIterator nodes = null;
            try
            {
                pivotGrid.BeginUpdate();
                XPathNavigator xNav = XMLHelper.CreateNavigator(GridConfig);
                nodes = xNav.Select("/Root/Columns/Column");
                if (nodes.Count > 0)
                {
                    foreach (XPathNavigator node in nodes)
                    {
                        GetNewField(pivotGrid, node);
                    }
                }
                pivotGrid.EndUpdate();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public static void Apply(PivotGridControl pivotGrid, String GridConfig)
        {
            if (String.IsNullOrEmpty(GridConfig))
            {
                return;
            }

            XPathNodeIterator nodes = null;
            Dictionary<String, PivotGridField> ColumnMap = new Dictionary<string, PivotGridField>();
            try
            {
                foreach (PivotGridField f in pivotGrid.Fields)
                {
                    ColumnMap.Add(f.FieldName, f);
                }

                pivotGrid.BeginUpdate();
                XPathNavigator xNav = XMLHelper.CreateNavigator(GridConfig);
                nodes = xNav.Select("/Root/Columns/Column");
                if (nodes.Count > 0)
                {
                    Type t = null;
                    foreach (XPathNavigator node in nodes)
                    {
                        String FieldName = node.SelectSingleNode("FieldName").Value;

                        if (ColumnMap.ContainsKey(FieldName))
                        {
                            PivotGridField field = ColumnMap[FieldName];
                            field.Visible = "1".Equals(node.SelectSingleNode("Visible").Value);

                            t = typeof(PivotArea);
                            field.Area = (PivotArea)Enum.Parse(t, node.SelectSingleNode("Area").Value);
                            field.AreaIndex = node.SelectSingleNode("AreaIndex").ValueAsInt;
                            field.Width = node.SelectSingleNode("Width").ValueAsInt;

                            t = typeof(DevExpress.Data.UnboundColumnType);
                            field.UnboundType = (DevExpress.Data.UnboundColumnType)Enum.Parse(t, node.SelectSingleNode("UnboundType").Value);
                            field.UnboundExpression = node.SelectSingleNode("UnboundExpression").Value;

                            t = typeof(DevExpress.Data.PivotGrid.PivotSummaryType);
                            field.SummaryType = (DevExpress.Data.PivotGrid.PivotSummaryType)Enum.Parse(t, node.SelectSingleNode("SummaryType").Value);

                            t = typeof(PivotTotalsVisibility);
                            field.TotalsVisibility = (PivotTotalsVisibility)Enum.Parse(t, node.SelectSingleNode("TotalsVisibility").Value);

                            field.CellFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                            field.CellFormat.FormatString = node.SelectSingleNode("FormatString").Value;

                            field.Options.ShowUnboundExpressionMenu = true;
                            field.Options.AllowEdit = false;
                        }
                    }
                }
                pivotGrid.EndUpdate();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

    }

}
